package com.lhkj.ct.admin.modules.gauge.scale.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.lhkj.ct.base.annntation.Log;
import com.lhkj.ct.base.annntation.PreventDuplicateSubmit;
import com.lhkj.ct.base.enums.ActionType;
import com.lhkj.ct.base.model.Option;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.base.utils.Asserts;
import com.lhkj.ct.base.validator.group.ValGroups;
import com.lhkj.ct.meta.modules.common.enums.PatientModule;
import com.lhkj.ct.meta.modules.gauge.common.enums.ConfigProcess;
import com.lhkj.ct.meta.modules.gauge.common.enums.DiagItemType;
import com.lhkj.ct.meta.modules.gauge.model.entity.TblDiagItem;
import com.lhkj.ct.meta.modules.gauge.model.entity.TblScale;
import com.lhkj.ct.meta.modules.gauge.model.vo.ScalePageVo;
import com.lhkj.ct.meta.modules.gauge.service.DiagItemService;
import com.lhkj.ct.meta.modules.gauge.service.ScaleConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2023-10-08
 */
@RestController
@RequestMapping("api/plat/gauge/scale")
@Api(tags = {"6.1.1量表配置"})
@Validated
public class ScaleConfigController {

    @Resource
    private ScaleConfigService scaleConfigService;

//    @Resource
//    private QrCodeService qrCodeService;

    @Resource
    private DiagItemService diagItemService;


    @GetMapping(value = "list")
    @ApiOperation(value = "列表接口")
    //@RequiresPermissions(value = {"system:scale:list"})
    public ActionResult<IPage<ScalePageVo>> list(TblScale entity, PageQuery pageQuery) {
        IPage<ScalePageVo> pageInfo = scaleConfigService.selectScalePage(entity, pageQuery);
        return ActionResult.success(pageInfo);
    }

    @GetMapping(value = "item/options")
    @ApiOperation(value = "项目下拉选项")
    @ApiOperationSupport(order = 1)
    public ActionResult<List<Option<String>>> options(TblDiagItem entity) {
        entity.setItemType(DiagItemType.MEASURE);
        return ActionResult.success(diagItemService.listOptions(entity));
    }

    @GetMapping(value = "checkName")
    @ApiOperation(value = "校验量表名称是否唯一")
    public ActionResult<Boolean> checkName(TblScale entity) {
        return ActionResult.success(scaleConfigService.checkGaugeNameUnique(entity));
    }


    @GetMapping(value = "{scaleId}/get")
    @ApiOperation(value = "量表基本信息")
    //@RequiresPermissions(value = {"system:scale:get", "system:scale:update"}, logical = Logical.OR)
    public ActionResult<TblScale> get(@PathVariable String scaleId) {
        return ActionResult.success(scaleConfigService.getForm(scaleId));
    }

    @PreventDuplicateSubmit
    @PostMapping(value = "insert")
    @ApiOperation(value = "新增量表")
    @Log(title = "量表配置", actionType = ActionType.INSERT)
    //@RequiresPermissions(value = {"system:scale:insert"})
    public ActionResult<TblScale> insert(@Validated(ValGroups.Insert.class) @RequestBody TblScale entity) {
        Asserts.assertTrue(scaleConfigService.checkGaugeNameUnique(entity), "量表名称已存在");
        entity.setProgress(ConfigProcess.BASIC);
        entity.setType(2);
        scaleConfigService.insert(entity);
        return ActionResult.success("量表保存成功", entity);
    }

    @PutMapping(value = "update")
    @ApiOperation(value = "修改量表")
    @Log(title = "量表配置", actionType = ActionType.UPDATE)
    //@RequiresPermissions(value = {"system:scale:update"})
    public ActionResult<Void> update(@Validated({ValGroups.Update.class}) @RequestBody TblScale entity) {
        TblScale target = scaleConfigService.getById(entity.getId());
        Asserts.assertFalse(null == target, "量表不存在");
        Asserts.assertTrue(scaleConfigService.checkGaugeNameUnique(entity), "量表名称已存在");
        entity.setType(target.getType());
        return ActionResult.judge(scaleConfigService.updateById(entity));
    }

    @DeleteMapping("delete")
    @ApiOperation(value = "删除量表", notes = "支持批量删除")
    @Log(title = "量表配置", actionType = ActionType.DELETE)
    //@RequiresPermissions(value = {"system:scale:delete"})
    public ActionResult<Void> delete(@RequestBody @NotEmpty(message = "删除的编号不能为空") List<String> ids) {
        int count = scaleConfigService.removeByIds(ids);
        return ActionResult.success("删除完成，共删除【" + count + "】条");
    }

    @GetMapping(value = "mould")
    @ApiOperation(value = "档案模板列表")
    //@RequiresPermissions(value = {"system:scale:get", "system:scale:update"}, logical = Logical.OR)
    public ActionResult<List<Option<Integer>>> mould() {
        List<Option<Integer>> collect = Arrays.stream(PatientModule.values()).map(m -> new Option<>(m.getValue(), m.getDesc())).collect(Collectors.toList());
        return ActionResult.success(collect);
    }

    @GetMapping(value = "{scaleId}/qrCode")
    @ApiOperation(value = "二维码接口")
    public ActionResult<String> getQrCode(@PathVariable String scaleId) {
        return ActionResult.success("操作成功",scaleConfigService.qrCode(scaleId));
    }

//    @Resource
//    private GaugeQuestionService questionService;
//
//    @Resource
//    private GaugeOptionService optionService;
//
//    @Resource
//    private DimensionService dimensionService;
//
//    @Resource
//    private DimQuestionService dimQuestionService;
//
//    @Resource
//    private ScaleDimNormService dimNormService;
//
//    @Resource
//    private ScaleWarnSettingService warnSettingService;
//
//    @Resource
//    private QutLogicService qutLogicService;
//
//    @Resource
//    private LogicAssoService logicAssoService;
//
//    @GetMapping(value = "motify")
//    @ApiOperation(value = "修改")
//    @Transactional
//    public ActionResult<Void> motify() {
//        Snowflake snowflake = new Snowflake();
//        // 量表
////        List<TblScale> tblScales = scaleConfigService.getBaseMapper().selectList(new LambdaQueryWrapper<TblScale>().select(TblScale::getId));
////        tblScales.forEach(p -> p.setTid(snowflake.nextIdStr()));
////        scaleConfigService.updateBatchById(tblScales);
//        // 维度
////        List<TblScaleDimension> tblScaleDimensions = dimensionService.getBaseMapper().selectList(new LambdaQueryWrapper<TblScaleDimension>().select(TblScaleDimension::getId));
////        tblScaleDimensions.forEach(p -> p.setTid(snowflake.nextIdStr()));
////        dimensionService.updateBatchById(tblScaleDimensions);
//        // 题目
////        List<TblGaugeQuestion> tblGaugeQuestions = questionService.getBaseMapper().selectList(new LambdaQueryWrapper<TblGaugeQuestion>().select(TblGaugeQuestion::getQutId));
////        tblGaugeQuestions.forEach(p -> p.setTid(snowflake.nextIdStr()));
////        questionService.updateBatchById(tblGaugeQuestions);
//        // 选项
////        List<TblGaugeOption> tblGaugeOptions = optionService.getBaseMapper().selectList(new LambdaQueryWrapper<TblGaugeOption>().select(TblGaugeOption::getOptId));
////        tblGaugeOptions.forEach(p -> p.setTid(snowflake.nextIdStr()));
////        optionService.updateBatchById(tblGaugeOptions);
//
//        // 维度关联题目
////        List<TblScaleDimQuestion> tblGaugeOptions = dimQuestionService.getBaseMapper().selectList(new LambdaQueryWrapper<TblScaleDimQuestion>().select(TblScaleDimQuestion::getId));
////        tblGaugeOptions.forEach(p -> p.setTid(snowflake.nextIdStr()));
////        dimQuestionService.updateBatchById(tblGaugeOptions);
//
//        // 解释说明，
////        List<TblScaleDimNorm> norms = dimNormService.getBaseMapper().selectList(new LambdaQueryWrapper<TblScaleDimNorm>().select(TblScaleDimNorm::getNormId));
////        norms.forEach(p -> p.setTid(snowflake.nextIdStr()));
////        dimNormService.updateBatchById(norms);
//
//        List<TblScaleWarnSetting> warnSettings = warnSettingService.getBaseMapper().selectList(new LambdaQueryWrapper<TblScaleWarnSetting>().select(TblScaleWarnSetting::getId));
//        warnSettings.forEach(p -> p.setTid(snowflake.nextIdStr()));
//        warnSettingService.updateBatchById(warnSettings);
//        return ActionResult.success();
//    }

}
